<?php

namespace App\Http\Controllers\Api;

use App\Coupons;
use App\Http\Services\FileManager;
use App\Http\Services\UserManager;
use App\User;
use App\UserCoupon;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class CouponController extends Controller
{
    /**
     * 优惠券列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCouponList(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
//        if(!$user)
//            return response()->json(['level' => 'error','message' => '请先登录!','data' => 'no_login']);

        /** @var Coupons $coupon */
        $coupons = Coupons::whereStatus(1)->orderBy('created_at','desc')->get();
        if(count($coupons) > 0){
            foreach($coupons as $coupon){
                if($user){
                    $userCoupon = UserCoupon::whereUserId($user->id)->whereCouponId($coupon->id)->first();
                    if($userCoupon){
                        $coupon->is_receive = 'yes';
                    }else{
                        $coupon->is_receive = 'no';
                    }
                } else {
                    $coupon->is_receive = 'no';
                }
                if ($coupon->img_id)
                    $coupon->img_url=FileManager::getImgUrl($coupon->img_id);
                else
                    $coupon->img_url=false;

            }
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $coupons]);
    }

    /**
     * 首页优惠券
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getHomeCoupon(Request $request){
        /** @var Coupons $coupon */
        $coupons = Coupons::whereStatus(1)->whereRemark('home')->orderBy('created_at','desc')->get();
        if(count($coupons) > 0){
            foreach($coupons as $coupon){
                $coupon->img_url=FileManager::getImgUrl($coupon->img_id);

                if($coupon->type=="direct"){
                    $coupon->coupon_type="无门槛优惠券";
                }

                if($coupon->type=="satisfy"){
                    $coupon->coupon_type="满减优惠券";
                }
            }
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $coupons]);
    }

    /**
     * 优惠券详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCouponDetail(Request $request){
        $coupon_id=!is_null($request->get('coupon_id'))?$request->get('coupon_id'):0;
        $user = UserManager::isLogin($request);
        if(!$user){

            $coupon=Coupons::whereId($coupon_id)->whereStatus(1)->first();

            $coupon->img_url=FileManager::getImgUrl($coupon->img_id);


            if($coupon->type=="direct"){
                $coupon->coupon_type="无门槛优惠券";
            }

            if($coupon->type=="satisfy"){
                $coupon->coupon_type="满减优惠券";
            }

        }else{
            $coupon=Coupons::whereId($coupon_id)->whereStatus(1)->first();
            if(!$coupon)
                return response()->json(['level' => 'error','message' => '找不到优惠券信息']);

            $userCoupon = UserCoupon::whereUserId($user->id)->whereCouponId($coupon->id)->first();

            if($coupon->type=="direct"){
                $coupon->coupon_type="无门槛优惠券";
            }

            if($coupon->type=="satisfy"){
                $coupon->coupon_type="满减优惠券";
            }

            $coupon->img_url=FileManager::getImgUrl($coupon->img_id);


            if($userCoupon){
                $coupon->is_receive = 'yes';
            }else{
                $coupon->is_receive = 'no';
            }
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $coupon]);
    }


    /**
     * 优惠券领取入口
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function couponReceive(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $couponId = !is_null($request->get('coupon_id')) ? $request->get('coupon_id') : 0;

        /** @var Coupons $thisCoupon */
        $thisCoupon = Coupons::whereId($couponId)->first();
        if(!$thisCoupon)
            return response()->json(['level' => 'error','message' => '该优惠券不存在！']);

        if($thisCoupon->status == 0)
            return response()->json(['level' => 'error','message' => '系统错误']);

        if(strtotime($thisCoupon->end_time) < time())
            return response()->json(['level' => 'error','message' => '优惠券领取时间已过']);

        $userCoupon = UserCoupon::whereUserId($user->id)->whereCouponId($thisCoupon->id)->first();
        if($userCoupon)
            return response()->json(['level' => 'error','message' => '您已经领取过了']);

        if($thisCoupon->num == 0)
            return response()->json(['level' => 'error','message' => '优惠券已经领取完，下次再来']);

        $thisCoupon->num = $thisCoupon->num - 1;
        $thisCoupon->save();

        $newUserCoupon = new UserCoupon();
        $newUserCoupon->user_id       = $user->id;
        $newUserCoupon->coupon_id     = $thisCoupon->id;
        $newUserCoupon->name          = $thisCoupon->name;
        $newUserCoupon->money         = $thisCoupon->money;
        $newUserCoupon->type          = $thisCoupon->type;
        $newUserCoupon->satisfy_money = $thisCoupon->satisfy_money;
        $newUserCoupon->start_time    = $thisCoupon->start_time;
        $newUserCoupon->end_time      = $thisCoupon->end_time;
        $newUserCoupon->save();

        return response()->json(['level' => 'success','message' => '领取成功','data' => $newUserCoupon]);
    }

    /**
     * 获取我的优惠券
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getUserCoupon(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录!','data' => 'no_login']);

        $type = !is_null($request->get('type')) && $request->get('type') != '' ? $request->get('type') : 'ha';

        if($type == 'unused'){
            $userCoupon = UserCoupon::whereUserId($user->id)->whereIsUse(0)->whereIsLocking(0)->where('end_time','>', date('Y-m-d H:i:s',time()))->get();
        }elseif($type == 'used'){
            $userCoupon = UserCoupon::whereUserId($user->id)->whereIsUse(1)->get();
        }elseif($type == 'expired'){
            $userCoupon = UserCoupon::whereUserId($user->id)->where('end_time','<=', date('Y-m-d H:i:s',time()))->get();
        }else{
            $userCoupon = UserCoupon::whereUserId($user->id)->get();
        }

        if(count($userCoupon) > 0){
            foreach($userCoupon as $item){
                if($item->type == "direct"){
                    $item->coupon_type = "无门槛优惠券";
                }

                if($item->type == "satisfy"){
                    $item->coupon_type = "满减优惠券";
                }

                $item->img_url = '';
                $coupon = Coupons::whereId($item->coupon_id)->first();
                if($coupon){
                    $item->img_url = FileManager::getImgUrl($coupon->img_id);
                }
            }
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $userCoupon]);
    }
}
